import 'package:find_friend/pages/xspace/model/artclemodel.dart';

class ArticleResponModel {
  PageModel data;

  ArticleResponModel({this.data});

  ArticleResponModel.fromJson(Map<String, dynamic> json) {
    data = json['data'] != null ? new PageModel.fromJson(json['data']) : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    return data;
  }
}

class PageModel {
  int total;
  int perPage;
  int currentPage;
  int lastPage;
  List<ArticleModel> data;

  PageModel({this.total, this.perPage, this.currentPage, this.lastPage, this.data});

  PageModel.fromJson(Map<String, dynamic> json) {
    total = json['total'];
    perPage = json['per_page'];
    currentPage = json['current_page'];
    lastPage = json['last_page'];
    if (json['data'] != null) {
      data = new List<ArticleModel>();
      json['data'].forEach((v) {
        data.add(new ArticleModel.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['total'] = this.total;
    data['per_page'] = this.perPage;
    data['current_page'] = this.currentPage;
    data['last_page'] = this.lastPage;
    if (this.data != null) {
      data['data'] = this.data.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class ArticleModel {
  int id;
  int schoolId;
  String title;
  String content;
  int createtime;
  int updatetime;
  String username;
  String mobile;
  String schoolname;
  Voice voice;
  String bio;
  String avatar;
  int upvotes;  // 赞
  int replys;   // 回复
  int reports; // 转发
  ReplyModel hot;
  List<Photo> photo;
  
  int followflag; // 是否关注
  String followRemark; // 备注名称
  int upvoteflag; //是否点赞.
  int dissflag; // 是否踩

  String get strUpvotes {
    return _complute(this.upvotes);
  }

  String get strReplys {
    return _complute(this.replys);
  }

  String get strReports {
    return _complute(this.reports);
  }

  String _complute(int count) {
    if(count == null) return "0";
    return count > 9999 ? ((count / 10000.0).toStringAsFixed(1) + "w") : count.toString();
  }

  // 获取用户显示的名称
  String get remarkname {
    if(username == null && followRemark == null) return mobile;
    return followRemark == null ? username : followRemark;
  }

  ArticleModel(
      {this.id,
      this.schoolId,
      this.title,
      this.content,
      this.createtime,
      this.updatetime,
      this.username,
      this.mobile,
      this.upvoteflag,
      this.schoolname,
      this.voice,
      this.hot,
      this.followflag,
      this.followRemark,
      this.dissflag,
      this.photo});

  ArticleModel.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    schoolId = json['school_id'];
    title = json['title'];
    content = json['content'];
    createtime = json['createtime'];
    updatetime = json['updatetime'];
    username = json['username'];
    mobile = json['mobile'];
    schoolname = json['schoolname'];
    bio = json['bio'];
    avatar = json['avatar'];
    upvotes = json['upvotes'];
    replys = json['replys'];
    reports = json['reports'];
    followflag = json['followflag'];
    followRemark = json['follow_remark'];
    upvoteflag = json['upvoteflag'];
    dissflag = json['dissflag'];
    voice = json['voice'] != null ? new Voice.fromJson(json['voice']) : null;
    hot = json['hot'] != null ? new ReplyModel.fromJson(json['hot']) : null;
    if (json['photo'] != null) {
      photo = new List<Photo>();
      json['photo'].forEach((v) {
        photo.add(new Photo.fromJson(v));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['school_id'] = this.schoolId;
    data['title'] = this.title;
    data['content'] = this.content;
    data['createtime'] = this.createtime;
    data['updatetime'] = this.updatetime;
    data['username'] = this.username;
    data['mobile'] = this.mobile;
    data['schoolname'] = this.schoolname;
    data['bio'] = this.bio;
    data['avatar'] = this.avatar;
    data['upvotes'] = this.upvotes;
    data['replys'] = this.replys;
    data['reports'] = this.reports;
    if (this.voice != null) {
      data['voice'] = this.voice.toJson();
    }
    if(this.hot != null) {
      data["hot"] = this.hot.toJson();
    }
    if (this.photo != null) {
      data['photo'] = this.photo.map((v) => v.toJson()).toList();
    }
    return data;
  }
}

class Voice {
  String url;
  int playtime;
  bool islocal; // 是否是本地音频

  Voice({this.url, this.playtime, this.islocal = false});

  Voice.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    playtime = json['playtime'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['url'] = this.url;
    data['playtime'] = this.playtime;
    return data;
  }
}

class Photo {
  String url;
  int playtime;
  String ownerUrl; // 图片对应的语音

  Photo({this.url, this.playtime, this.ownerUrl});

  Photo.fromJson(Map<String, dynamic> json) {
    url = json['url'];
    playtime = json['playtime'];
    ownerUrl = json['owner_url'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['url'] = this.url;
    data['playtime'] = this.playtime;
    data['owner_url'] = this.ownerUrl;
    return data;
  }
}